package dimensional.traveler.server.guice;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

import dimensional.traveler.client.services.CareerService;
import dimensional.traveler.client.services.MenuImageService;
import dimensional.traveler.client.services.UserService;
import dimensional.traveler.server.persistence.CareerRepository;
import dimensional.traveler.server.persistence.CharacterClassRepository;
import dimensional.traveler.server.persistence.MissionRepository;
import dimensional.traveler.server.persistence.UserRepository;
import dimensional.traveler.server.persistence.mock.MockCareerRepository;
import dimensional.traveler.server.persistence.mock.MockCharacterClassRepository;
import dimensional.traveler.server.persistence.mock.MockMissionRepository;
import dimensional.traveler.server.persistence.mock.MockUserRepository;
import dimensional.traveler.server.services.CareerServiceImpl;
import dimensional.traveler.server.services.MenuImageServiceImpl;
import dimensional.traveler.server.services.UserServiceImpl;
import dimensional.traveler.server.services.servlets.CareerServiceServlet;
import dimensional.traveler.server.services.servlets.CharacterClassServiceImpl;
import dimensional.traveler.server.services.servlets.LoginServiceImpl;
import dimensional.traveler.server.services.servlets.MenuImageServiceServlet;
import dimensional.traveler.server.services.servlets.ServiceAggregatorImpl;
import dimensional.traveler.server.services.servlets.UserServiceServlet;

public class ServletConfig extends GuiceServletContextListener {

	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new ServletModule(){
			
			@Override
			protected void configureServlets() {
				bindServerSideServices();
				bindServlets();
				bindRepositories();
			}

			private void bindServerSideServices() {
				bind(MenuImageService.class).to(MenuImageServiceImpl.class).in(Singleton.class);
				bind(UserService.class).to(UserServiceImpl.class).in(Singleton.class);
				bind(CareerService.class).to(CareerServiceImpl.class).in(Singleton.class);
			}

			private void bindRepositories() {
				bind(UserRepository.class).to(MockUserRepository.class).in(Singleton.class);
				bind(CharacterClassRepository.class).to(MockCharacterClassRepository.class).in(Singleton.class);
				bind(CareerRepository.class).to(MockCareerRepository.class).in(Singleton.class);
				bind(MissionRepository.class).to(MockMissionRepository.class).in(Singleton.class);
			}

			private void bindServlets() {
				bind(LoginServiceImpl.class).in(Singleton.class);
				serve("/dimensionaltraveler/login").with(LoginServiceImpl.class);
				
				bind(MenuImageServiceServlet.class).in(Singleton.class);
				serve("/dimensionaltraveler/menuimage").with(MenuImageServiceServlet.class);
				
				bind(CharacterClassServiceImpl.class).in(Singleton.class);
				serve("/dimensionaltraveler/characterclasses").with(CharacterClassServiceImpl.class);
				
				bind(CareerServiceServlet.class).in(Singleton.class);
				serve("/dimensionaltraveler/careers").with(CareerServiceServlet.class);
				
				bind(UserServiceServlet.class).in(Singleton.class);
				serve("/dimensionaltraveler/users").with(UserServiceServlet.class);
				
				bind(ServiceAggregatorImpl.class).in(Singleton.class);
				serve("/dimensionaltraveler/aggregator").with(ServiceAggregatorImpl.class);
				
			}
			
		});
	}

}
